package com.hdugraduate;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class StartCamera extends Activity implements SurfaceHolder.Callback{
	protected static final String TAG = null;
	String filePathName=null;
	SurfaceView mSurfaceView;
	SurfaceHolder mSurfaceHolder;
	Camera mCamera;
	boolean mPreviewRunning = false;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.camera);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// TODO Auto-generated method stub
		if (mPreviewRunning) {
			mCamera.stopPreview();
		}
		Camera.Parameters p = mCamera.getParameters();
		p.setPictureFormat(PixelFormat.JPEG);
		p.setPreviewSize(480, 320);
		p.setPictureSize(480, 320);
		mCamera.setParameters(p);
		try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			e.printStackTrace();
		}
		mCamera.startPreview();
		mPreviewRunning = true;
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mCamera = Camera.open();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mCamera.stopPreview();
		mPreviewRunning = false;
		mCamera.release();
	}
	private Camera.ShutterCallback shutterCallback = new ShutterCallback()
	  { 
	    public void onShutter() 
	    { 
	      // Shutter has closed 
	    } 
	  }; 
	  private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

			public void onPictureTaken(byte[] data, Camera camera) {
				// TODO Auto-generated method stub
				Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
				checkFileName();
				File myCaptureFile = new File(filePathName);
				try{
					BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
					bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
					bos.flush();
					bos.close();	
				}catch (Exception e){Log.e(TAG, e.getMessage());}
			}
			
		};
	  private Camera.PictureCallback rawCallback = new Camera.PictureCallback() 
	  { 
	    public void onPictureTaken(byte[] _data, Camera _camera) 
	    { 
	      // TODO Handle RAW image data 
	    } 
	  }; 
	  
	  private void checkFileName(){
		  
		  Calendar calCurrent = Calendar.getInstance();
		  int intDay = calCurrent.get(Calendar.DATE);
		  int intMonth = calCurrent.get(Calendar.MONTH) + 1;
		  int intYear = calCurrent.get(Calendar.YEAR);
		  Date dd = new Date();
		  int intHour = dd.getHours();
		  int intMints = dd.getMinutes();
		  int intSecond = dd.getSeconds();
		  filePathName="/sdcard/TN/Photos/"+String.valueOf(intYear) + String.valueOf(intMonth) + String.valueOf(intDay)+String.valueOf(intHour)+String.valueOf(intMints)+String.valueOf(intSecond) + ".jpg";
	  }
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE,0,0,"Setting");
		menu.add(Menu.NONE,1,1,"About");
		menu.add(Menu.NONE,2,2,"Quit");
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()){
    		case 0:
    			mCamera.takePicture(shutterCallback, rawCallback, mPictureCallback);
    			mCamera.stopPreview();
    			mCamera.startPreview();
    			break;
    		case 1:
    			break;
    		case 2:
    			finish();
    			break;
    	}
    	return true;
    }
}
